STEP 5: Notes That Sustain Only During Mouseover

We have seen how the playNote method can be used to trigger sounds when certain user interface events such as mouseovers and mouseclicks occur. When the playNote method is called, two events occur in the Beatnik player: the desired note is turned on, and then it is turned off again after the specified duration. Both of these events can also be triggered directly using the Music Object's noteOn and noteOff methods. This kind of control allows you to have a note that sustains indefinitely until a particular user interface event occurs. For instance, when the user mouses over a graphic, a note can be turned on. And then, when they mouse out of the graphic again, the note can be turned off. This is exactly the application for noteOn and noteOff that is used in this step of the tutorial.

PREV HOME NEXT

Interact with the sample for a while and get a feel for the different response. If necessary, go back to the previous step to compare behaviors. You will find that the new approach gives a more immediate sense of leaving each active area of the navigation bar. Also, because the note is being sustained indefinitely while you are over an active region, you are always aware that the region is clickable. Compare this behavior to the behavior of the previous step, where the note eventually fades out, even while you remain over a certain area.

Now, let's look at the source...

Copy-and-Paste
<SCRIPT SRC="music-object.js"></SCRIPT>

<SCRIPT LANGUAGE=JavaScript><!-- //

notePlayer = new Music ();
notePlayer.setMonophonic (1,true);
notePlayer.stubEmbed ('stub.rmf');

// --></SCRIPT>

<CENTER>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0>
   <TR>
      <TD>
         <A
            HREF="buckingham1.html"
            ONMOUSEOVER="notePlayer.noteOn (1,1,98,'C4',127)"
            ONMOUSEOUT="notePlayer.noteOff (1,'C4')"
            ONCLICK="notePlayer.playNote (1,0,213,'C4',127,1000)"
         >
         <IMG
            SRC="navbar-prev.gif"
            WIDTH=96
            HEIGHT=36
            ALT="PREV"
            BORDER=0
            HSPACE=0></A></TD>
      <TD>
         <A
            HREF="buckingham-home.html"
            ONMOUSEOVER="notePlayer.noteOn (1,1,98,'D4',127)"
            ONMOUSEOUT="notePlayer.noteOff (1,'C4')"
            ONCLICK="notePlayer.playNote (1,0,213,'C4',127,1000)"
         >
         <IMG
            SRC="navbar-home.gif"
            WIDTH=302
            HEIGHT=36
            ALT="HOME"
            BORDER=0
            HSPACE=0></A></TD>
      <TD>
         <A
            HREF="buckingham3.html"
            ONMOUSEOVER="notePlayer.noteOn (1,1,98,'E4',127)"
            ONMOUSEOUT="notePlayer.noteOff (1,'C4')"
            ONCLICK="notePlayer.playNote (1,0,213,'C4',127,1000)"
         >
         <IMG
            SRC="navbar-next.gif"
            WIDTH=96
            HEIGHT=36
            ALT="NEXT"
            BORDER=0
            HSPACE=0></A></TD>
   </TR>
</TABLE>
</CENTER>

You will see from the source that the playNote method calls from the previous step have now been replaced with noteOn method calls for the ONMOUSEOVER events. And we have also now added noteOff method calls for the ONMOUSEOUT events, so that the notes do not sustain forever but get switched off when the user mouses out of each graphic. The parameters that the noteOn method takes are exactly the same as the parameters for the playNote method, with the notable absence of a NoteDurationMsINT parameter.

By contrast, the noteOff method takes just two parameters: MidiChannel1to16, and NoteNameSTR. In order to successfully turn off a note that is turned on with an ONMOUSEOVER event, the noteOff method must match both the MIDI channel and note name values with the values that were sent in the noteOn method call. When you are fine-tuning a page's sonification and trying out different note values to find the right "sound" for your page, it is easy to make the mistake of changing just the note value in the ONMOUSEOVER event and omitting to change the value similarly in the ONMOUSEOUT event. When this happens, notes will not be switched off properly. So, each time you make such a change, you need to make the change in two places.

With the ONCLICK event, we still have the playNote method call from the previous step. Only now, the effect of the setMonophonic statement at the head of the code is that much more noticable. The percussive sound that is triggered when the user clicks on one of the links is being played through the same channel as the mouseover sounds. Because the channel has been set to monophonic mode, as soon as the click sound is triggered, the mouseover sound is killed. This is appropriate because the user has been "hovering" over a potential link without committing to any action. The mouseover drone then acts as a "soundtrack" to their state of indecision. As soon as a decision is reached and the user clicks, the sound of the indecision is killed with a decisive click noise.